home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swaga_c.zip / CMDLINE.SWG / 0011_Command Line Unit.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-27  |  9KB  |  266 lines

  1. unit CmdLin;
  2. (*
  3.    This unit will process command line flags, (/N -N)
  4.         a) as present or absent (Is_Param)
  5.         b) with an integer (eg. /N54 /X-76) (Param_Int)
  6.         c) with a real number (eg /J-987.65) (Param_Real)
  7.         d) with strings, including delimited strings with embedded spaces
  8.            ( eg. /X"This is the story!" /YFred)
  9.  
  10.       Routines are included to count and return the parameters that
  11.       aren't flags (Non_Flag_Count), and to return them without
  12.       counting the flag parameters (Non_Flag_Param).
  13.  
  14.       So ( /X76 Filename.txt /N"My name is Fred." George ) would count
  15.       two non-flag params, #1 = filename.txt and #2 = george.
  16.  
  17.       This is completely public domain, all I want in return for your use
  18.       is appreciation.  If you improve this unit, please let me know.
  19.       Some possible improvements would be to allow embedded strings in
  20.       non-flag parameters.  I haven't done this because I haven't needed
  21.       it.
  22.  
  23.  
  24.       Jim Walsh      CIS:72571,173
  25.  
  26. *)
  27.  
  28. INTERFACE
  29.  
  30.   function Is_Param(flag:Char) : Boolean;
  31.   { Responds yes if the flag (ie N) is found in the command line (ie /N or -N) }
  32.  
  33.   function Param_Int(flag:Char) : LongInt;
  34.   { Returns the integer value after the parameter, ie -M100, or -M-123 }
  35.  
  36.   function Param_Real(flag:Char) : Real;
  37.   { Returns the Real value after the parameter, ie -X654.87, or -x-3.14159 }
  38.  
  39.   function Param_Text(flag:Char) : String;
  40.   { Returns the string after the parameter, ie -MHello -> 'Hello',            }
  41.   {  -m"This is it, baby" -> 'This is it, baby', valid string delims='' "" [] }
  42.  
  43.   function Non_Flag_Param(index:integer) : string;
  44.   { Returns the indexth parameter, not preceded with a flag delimeter }
  45.   { /X Text.txt /Y876.76 /G"Yes sir!" MeisterBrau /?                  }
  46.   { For this command line 'Text.txt' is Non Flag Param #1,            }
  47.   {    and 'MeisterBrau is #2.                                        }
  48.   { NB: Delimeted Non flag parameters (eg "Meister Brau")             }
  49.   {  not currently supported.                                         }
  50.  
  51.   function Non_Flag_Count : integer;
  52.   { Returns the number of non-flag type parameters }
  53.  
  54.  
  55. IMPLEMENTATION
  56. const
  57.   flag_delims   : Set of Char = ['/','-'];
  58.   no_of_string_delims = 3;
  59. type
  60.   string_delim_type = Array[1..3] of record
  61.                                        start, stop : char
  62.                                      end;
  63. const
  64.   string_delims : string_delim_type = ((start:#39; stop:#39),
  65.                                        (start:#34; stop:#34),
  66.                                        (start:'['; stop:']'));
  67.  
  68.  
  69. function LowerCaseChar(c:char):char;
  70. begin
  71.   if (c>='A') and (c<='Z') Then LowerCaseChar:=Char(Ord(c)+$20)
  72.                            Else LowerCaseChar:=c;
  73. end;
  74.  
  75.  
  76. {----------------------------------------------------------------------------}
  77.   function WhereFlagOccurs(flag:Char) : integer;
  78.   {  returns the index number of the paramter where the flag occurs  }
  79.   {  if the flag is never found, it returns 0                        }
  80.   var
  81.     ti1      : integer;
  82.     finished : boolean;
  83.     paramcnt : integer;
  84.     ts1      : string;
  85.   begin
  86.     flag:=LowerCaseChar(flag);
  87.     finished:=false;
  88.     ti1:=1;
  89.     paramcnt:=ParamCount;
  90.     While Not(finished) Do begin
  91.       If ti1>paramcnt Then begin
  92.         finished:=true;
  93.         ti1:=0;
  94.       end Else begin
  95.         ts1:=ParamStr(ti1);
  96.         If (ts1[1] In flag_delims) AND (LowerCaseChar(ts1[2])=flag) Then finished:=true;
  97.       end;
  98.       If Not(finished) Then Inc(ti1);
  99.     end; {While}
  100.     WhereFlagOccurs:=ti1;
  101.   end;
  102.  
  103. {----------------------------------------------------------------------------}
  104.   function Is_Param(flag:Char) : Boolean;
  105.   begin
  106.     If WhereFlagOccurs(flag)=0 Then Is_Param:=false Else Is_Param:=true;
  107.   end;
  108.  
  109. {----------------------------------------------------------------------------}
  110.   function Param_Int(flag:Char) : LongInt;
  111.   var
  112.     param_loc : integer;
  113.     result    : longint;
  114.     ts1       : string;
  115.     ti1       : integer;
  116.   begin
  117.     param_loc:=WhereFlagOccurs(flag);
  118.     If param_loc=0 Then result:=0
  119.     Else begin
  120.       ts1:=ParamStr(param_loc);     { Get the string }
  121.       ts1:=Copy(ts1,3,255);         { Get rid of the delim and the flag }
  122.       Val(ts1,result,ti1);          { Make the value }
  123.       If ti1<>0 Then result:=0;     { Make sure there is no error }
  124.     end; {If/Else}
  125.     Param_Int:=result
  126.   end;
  127.  
  128. {----------------------------------------------------------------------------}
  129.   function Param_Real(flag:Char) : Real;
  130.   var
  131.     param_loc : integer;
  132.     result    : real;
  133.     ts1       : string;
  134.     ti1       : integer;
  135.   begin
  136.     param_loc:=WhereFlagOccurs(flag);
  137.     If param_loc=0 Then result:=0.0
  138.     Else begin
  139.       ts1:=ParamStr(param_loc);     { Get the string }
  140.       ts1:=Copy(ts1,3,255);         { Get rid of the delim and the flag }
  141.       Val(ts1,result,ti1);          { Make the value }
  142.       If ti1<>0 Then result:=0.0;   { Make sure there is no error }
  143.     end; {If/Else}
  144.     Param_Real:=result;
  145.   end;
  146.  
  147. {----------------------------------------------------------------------}
  148.   function Which_String_Delim(S:string) : byte;
  149.   { Returns the index of the strings first character in the array
  150.     of string_delims, if the first char of S isn't a delim it returns 0 }
  151.   var
  152.     tc1 : char;
  153.     tb1 : byte;
  154.     finished : boolean;
  155.     result   : byte;
  156.   begin
  157.     tc1:=S[1];
  158.     tb1:=1;
  159.     finished:=false;
  160.     While Not(finished) Do begin
  161.       If tb1>no_of_string_delims Then begin
  162.         result:=0;
  163.         finished:=true;
  164.       end Else begin
  165.         If tc1=string_delims[tb1].start Then begin
  166.           result:=tb1;
  167.           finished:=true;
  168.         end;
  169.       end;
  170.       If Not(finished) Then Inc(tb1);
  171.     end; {While}
  172.     Which_String_Delim:=result;
  173.   end; {function Which_String}
  174.  
  175. {-------------------------------------------------------------------------}
  176.   function Param_Text(flag:Char) : String;
  177.   var
  178.     param_loc : integer;
  179.     param_cnt : integer;
  180.     result    : string;
  181.     ts1       : string;
  182.     ti1       : integer;
  183.     s_delim   : byte;          { This should be 0(no string), 1', 2", 3[ }
  184.     finished  : boolean;
  185.   begin
  186.     param_loc:=WhereFlagOccurs(flag);
  187.     If param_loc=0 Then result:=''
  188.     Else begin
  189.       ts1:=ParamStr(param_loc);     { Get the string }
  190.       ts1:=Copy(ts1,3,255);         { Get rid of the delim and the flag }
  191.       { See if the first char of ts1 is one of the string_delims }
  192.       s_delim:=Which_String_Delim(ts1);
  193.       If s_delim=0 Then result:=ts1
  194.       Else begin
  195.         result:=Copy(ts1,2,255);    { Drop the s_delim }
  196.         finished:=false;
  197.         param_cnt:=ParamCount;
  198.         While Not(finished) Do begin
  199.           Inc(param_loc);
  200.           If param_loc>param_cnt Then finished:=true
  201.           Else begin
  202.             ts1:=ParamStr(param_loc);
  203.             If ts1[Length(ts1)]=string_delims[s_delim].stop Then finished:=true;
  204.             result:=result+' '+ts1;
  205.           end; { If/Else }
  206.         end; { While }
  207.         result[0]:=Char(Length(result)-1);      { Drop the last delimeter }
  208.       end; { If/Else a delimited string }
  209.     end; { If/Else the flag is found }
  210.     Param_Text:=result;
  211.   end;
  212.  
  213. {---------------------------------------------------------------------------}
  214.   function Non_Flag_Param(index:integer) : string;
  215.   var
  216.     param_cnt : integer;
  217.     ti1       : integer;
  218.     ts1       : string;
  219.     finished  : boolean;
  220.     cur_index : integer;
  221.   begin
  222.     param_cnt:=ParamCount;
  223.     cur_index:=0;
  224.     ti1:=0;
  225.     finished:=false;
  226.     While Not(finished) Do begin
  227.       Inc(ti1);
  228.       IF cur_index>param_cnt Then begin
  229.         ts1:='';
  230.         finished:=true;
  231.       end Else begin
  232.         ts1:=ParamStr(ti1);
  233.         If Not(ts1[1] IN flag_delims) Then begin
  234.           Inc(cur_index);
  235.           If cur_index=index Then finished:=true;
  236.         end;
  237.       end; {If/Else}
  238.     end; {While}
  239.     Non_Flag_Param:=ts1;
  240.   end;
  241.  
  242. {---------------------------------------------------------------------------}
  243.   function Non_Flag_Count : integer;
  244.   var
  245.     param_cnt : integer;
  246.     result    : integer;
  247.     ti1       : integer;
  248.     ts1       : string;
  249.   begin
  250.     param_cnt:=ParamCount;
  251.     result:=0;
  252.     ti1:=0;
  253.     For ti1:=1 To param_cnt Do begin
  254.       ts1:=ParamStr(ti1);
  255.       If Not(ts1[1] IN flag_delims) Then begin
  256.         Inc(result);
  257.       end;
  258.     end; {For}
  259.     Non_Flag_Count:=result;
  260.   end;
  261.  
  262.  
  263.  
  264.  
  265. END.
  266.